package com.interviews.threadlocal;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 所有的线程都共用一个 simpleDateFormat 对象
 *
 * @author qian
 * @version 1.0
 * @date 2022/3/3 16:29
 */
public class ThreadLocalDemo04 {


    public static ExecutorService threadPool = new ThreadPoolExecutor(16, 16, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(), new ThreadLocalDemo03.ThreadLocalDemo03ThreadFactory("SimpleDateFormat04"));


    static SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss");


    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 1000; i++) {

            int finalI = i;

            threadPool.submit(() -> {

                String date = new ThreadLocalDemo04().date(finalI);

                System.out.println(date);

            });

        }

        threadPool.shutdown();

    }


    public String date(int seconds) {

        Date date = new Date(1000 * seconds);

        return dateFormat.format(date);

    }

}

